﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TechTree.Stalk.Roots.Configuration.Components
{
    public class ComponentsConfiguration : ConfigurationSection
    {
        #region Constants
        private const string CONFIGURATION_PROPERTY_COLLECTION_GLOBALIZATION_COMPONENT = "globalization";
        private const string CONFIGURATION_SECTION = "techtree.components";
        #endregion

        [ConfigurationProperty(CONFIGURATION_PROPERTY_COLLECTION_GLOBALIZATION_COMPONENT, IsDefaultCollection = true, IsKey = false, IsRequired = true)]
        public ComponentCollection Globalization
        {
            get
            {
                return base[CONFIGURATION_PROPERTY_COLLECTION_GLOBALIZATION_COMPONENT] as ComponentCollection;
            }
        }

        /// <summary>
        /// Gets the configuration section.
        /// </summary>
        public static ComponentsConfiguration GetConfig()
        {
            try
            {
                return (ComponentsConfiguration)ConfigurationManager.GetSection(CONFIGURATION_SECTION);
            }
            catch (Exception ex)
            {
                throw new Exception();//throw new ExcecaoArquiteturaConfiguracao(ConfiguracaoArquitetura.ERRO_CONFIG_COMPONENTES, ConfiguracaoArquitetura.CODIGO_10040, ex);
            }
        }
    }

    public class ComponentCollection : ConfigurationElementCollection
    {
        #region Constantes
        private const string COMPONENT_ELEMENT_NAME = "component";
        #endregion

        protected override ConfigurationElement CreateNewElement()
        {
            return new ComponentElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ComponentElement)element).Name;
        }
        protected override string ElementName
        {
            get
            {
                return COMPONENT_ELEMENT_NAME;
            }
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        public ComponentElement this[int index]
        {
            get
            {
                return base.BaseGet(index) as ComponentElement;
            }
        }
        public ComponentElement this[string name_]
        {
            get
            {
                return base.BaseGet(name_) as ComponentElement;
            }
        }
    }

    public class ComponentElement : ConfigurationElement
    {
        #region Constants
        private const string PROPERTY_CONFIG_NAME = "name";
        private const string PROPERTY_CONFIG_CLASS = "class";
        #endregion

        [ConfigurationProperty(PROPERTY_CONFIG_NAME, IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return base[PROPERTY_CONFIG_NAME] as string; }
            //set { base[PROPERTY_CONFIG_NAME] = value; }
        }

        [ConfigurationProperty(PROPERTY_CONFIG_CLASS, IsRequired = true, IsKey = false)]
        public string Class
        {
            get { return base[PROPERTY_CONFIG_CLASS] as string; }
            //set { base[PROPERTY_CONFIG_CLASS] = value; }
        }
    }
}
